package com.ekeyman.securecreditlib.domain;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.ekeyman.securecreditlib.annotations.Encrypt;
@Entity
@Table(name = "CARDTRANSACTIONS")
public class CardTransaction implements Serializable {
	private static final long serialVersionUID = -2136796068602504459L;
	private long id;
	
	@Encrypt
	private String transactionNumber;
	
	private String vendorCode;
	private String transactionDate;
	private String postDate;
	private BigDecimal amount;
	
	private long version;
	private CreditCard creditCard;
	
	public void setId(long id) {
		this.id = id;
	}
	
	@Id @GeneratedValue
	public long getId() {
		return id;
	}
	
	public void setTransactionNumber(String transactionNumber) {
		this.transactionNumber = transactionNumber;
	}
	
	@Column(name="TRANSACTIONUMBER", nullable=false)
	public String getTransactionNumber() {
		return transactionNumber;
	}
	
	public void setVendorCode(String vendorCode) {
		this.vendorCode = vendorCode;
	}
	
	@Column(name="VENDORCODE", nullable=false)	
	public String getVendorCode() {
		return vendorCode;
	}
	
	public void setTransactionDate(String transactionDate) {
		this.transactionDate = transactionDate;
	}
	
	@Column(name="TRANSACTIONDATE", nullable=false)	
	public String getTransactionDate() {
		return transactionDate;
	}
	
	public void setPostDate(String postDate) {
		this.postDate = postDate;
	}
	
	@Column(name="POSTDATE", nullable=false)		
	public String getPostDate() {
		return postDate;
	}
	
	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}
	
	@Column(name="AMOUNT", nullable=false)	
	public BigDecimal getAmount() {
		return amount;
	}
	
	public void setVersion(long version) {
		this.version = version;
	}
	
	@Column(name="VERSION")	
	public long getVersion() {
		return version;
	}
	
	public void setCreditCard(CreditCard creditCard) {
		this.creditCard = creditCard;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "CREDITCARDID")
	public CreditCard getCreditCard() {
		return creditCard;
	}
	
	@Transient
	@Override
	public boolean equals(Object o){
		boolean result = false;
		if (o instanceof CardTransaction){
			CardTransaction ct = (CardTransaction)o;
			if(getVendorCode().equalsIgnoreCase(ct.getVendorCode()) &&
					getTransactionDate().equalsIgnoreCase(ct.getTransactionDate()) &&
					getPostDate().equalsIgnoreCase(ct.getPostDate())) {
				result = true;
			}
		}
		return result;
	}

	@Transient
	@Override
	public int hashCode(){
		return getVendorCode().hashCode() +
			getTransactionDate().hashCode() + 
			getPostDate().hashCode();
	}
}
